<?php
header("Content-Type: text/html; charset=iso-8859-1");
require 'include.php';
$_SESSION['navigation'] = 'historique';
$_SESSION['cat'] = 'historique';
$periode = (!empty($_REQUEST['periode'])) ? $_REQUEST['periode'] : "";
$saison = (!empty($_REQUEST['saison'])) ? $_REQUEST['saison'] : "";
$lien = (!empty($_REQUEST['lien'])) ? $_REQUEST['lien'] : "";
$article = (!empty($_REQUEST['article'])) ? $_REQUEST['article'] : "";
$search = (!empty($_REQUEST['search'])) ? $_REQUEST['search'] : "";
if($lien){
	$article = $lien;
	$search = 1;
}
$periodes = array("1976-1988" => "76",
				"1988-1996" => "88",
				"1996-2001" => "96",
				"2001-2012" => "01",
				"2012-" => "12");

if($periode){
	if(!$saison) {	echo "<h2>$periode</h2>\n";
	   $contenu = fread(fopen("images/historique/presse_$periode.txt", "r"), filesize("images/historique/presse_$periode.txt"));
	   echo "<p class = 'center fade'>Cliquer sur la date pour acc&eacute;der &agrave; la revue de presse</p>\n";
	    echo "<div style = 'text-align: justify; line-height: 1.4em;font-weight: bold;'>".(valid_chars($contenu))."</div>\n";
	     $tab = split('-',$periode);
	   if($tab[1]=='') $tab[1]=date('Y');
	   if($tab[1]==$tab[0]) $tab[1]++;
	   for($i=$tab[0]; $i <= $tab[1];$i++){
	       $j=$i+1;
	       $per = $i."-".$j;
	       $requete = "select * from presse where periode = '$per' and valide = 1";
	       $resultat = ExecRequete($requete);
	       if($sortie = LigneSuivante($resultat)){
	           echo "<div class = 'resume_presse'>\n";
	               $requete = "select count(*) as nb from historique where periode = '$per'";
	               $resultat = ExecRequete($requete);
	               $sortie_nb = LigneSuivante($resultat);
	               $print_periode = substr_replace($per,'',-4,-2);
	               if($sortie_nb->nb) $print = "<a href = 'javascript:historique_presse(\"$periode\",\"$per\")'>$print_periode</a>\n";
	               else $print = $print_periode;
	               echo "<div class = 'desc_periode'>$print&nbsp;&nbsp;&nbsp;".(prepare_info($sortie->description))."</div>\n";
	           echo "</div>\n";
	       }
    
	   }    
	}    
	else{
		$tab_saison = explode("-",$saison);
		$suivant = "";
		$precedent = "";
		foreach($periodes as $print => $start){
			$tab_periode = explode("-",$print);
			if(!$tab_periode[1]) $tab_periode[1] = (date('m') > 7) ? date('Y')+1 : date('Y');
			if($tab_saison[0] >=  $tab_periode[0] && $tab_saison[0] < $tab_periode[1]){
				$precedent = ($tab_saison[0] > $tab_periode[0]) ? "<a style = 'padding-right: 5px; cursor: pointer;font-weight: bold;' href = 'javascript:historique_presse(\"$print\",\"".($tab_saison[0]-1)."-$tab_saison[0]\")'>&larr;</a>" : "<span style = 'width: 21px'>&nbsp;</span>";
				$suivant = ($tab_saison[1] < $tab_periode[1]) ? "<a href =  'javascript:historique_presse(\"$print\",\"$tab_saison[1]-".($tab_saison[1]+1)."\")' style = 'padding-left: 5px; cursor: pointer;font-weight: bold;'>&rarr;</a>" : "<span style = 'width: 21px'>&nbsp;</span>";
			}
		}
		
		echo "<h2>$precedent $saison $suivant</h2>\n";
		$table = new table();
		$fields = array("id","date","titre");
		foreach($fields as $field){
			$$field = new table_field($field);
			if($field == 'date') $$field->styleclass = 'center';
			elseif($field == 'id') $$field->hidden = 1;
			$table->fields[] = $$field;
		}
		$table->styleclass = 'bold_hover';
		$requete = "select * from historique where periode = '$saison' and valide = 1 order by date";
		$resultat = ExecRequete($requete);
		while($sortie = LigneSuivante($resultat)){
			$row = new table_row();
			$row->onclick = "onclick = \"historique_article($sortie->id);self.location.href='#article';\" style = \"cursor:pointer\"";
			foreach($fields as $field){
				if($field == 'titre'){
					$sortie->$field = (ereg("<h2>(.*)</h2>",$sortie->corps,$regs)) ? $regs[1] : $sortie->titre;
					$sortie->$field = valid_chars($sortie->$field);
				}
				$row->cols[$field] = $sortie->$field;
			}
			$table->rows[] = $row;
		}
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer();
		echo "<a name = 'article'></a><div id = 'article_historique'></div>\n";
	}	
}
elseif($article){
	$historique = new historique($article);
	if($historique->valide){
		if($search){ 
			$script = "function historique_presse(periode,saison){\n";
			$script .= "	$('top').scrollTo();\n";
			$script .= " new Ajax.Updater('content_historique','historique.php',{method: 'get', parameters: {periode: periode,saison: saison}})\n";
			foreach($periodes as $year => $suffix){
				$script .= "if(periode != '$year'){\n";
				$script .= "	if($('img_periode_$year')) $('img_periode_$year').src = 'images/layout/historique".$suffix."_fade.png';\n";
				$script .= "}\n";
				$script .= "else{\n";
				$script .= "	if($('img_periode_$year')) $('img_periode_$year').src = 'images/layout/historique".$suffix.".png';\n";
				$script .= "}\n";
			}
			$script .= "}\n";
			$script .= "function historique_periode(periode){\n";
			$script .= " new Ajax.Updater('content_historique','historique.php',{method: 'get',parameters: {periode: periode}})\n";
			foreach($periodes as $year => $suffix){
				$script .= "if(periode != '$year'){\n";
				$script .= "	if($('img_periode_$year')) $('img_periode_$year').src = 'images/layout/historique".$suffix."_fade.png';\n";
				$script .= "}\n";
				$script .= "else{\n";
				$script .= "	if($('img_periode_$year')) $('img_periode_$year').src = 'images/layout/historique".$suffix.".png';\n";
				$script .= "}\n";
			}
			$script .= "}\n";
			
			
			entete('historique',$script);
			echo "<h1>Historique</h1>\n";
			foreach($periodes as $print => $start){
				$tab_periode = explode("-",$print);
				$tab_saison = explode("-",$historique->periode);
				if(!$tab_periode[1]) $tab_periode[1] = (date('m') > 7) ? date('Y')+1 : date('Y');
				if($tab_saison[0] >=  $tab_periode[0] && $tab_saison[0] < $tab_periode[1]){
					$periode = $print;
				}
			}
			echo "<div id = 'bouton_historique'>\n";
			echo "<ol class = 'boutons'>\n";
			foreach($periodes as $year => $suffix){
				echo "<li class = 'bouton_large pointer' onclick = 'historique_periode(\"$year\")'>$year<br /><img  id = 'img_periode_$year' src = 'images/layout/historique$suffix.png' alt  = '$year' width = '60' height = '60'  style = 'cursor: pointer' /></li>\n";
			}
			echo "</ol>\n";
			echo "<br class = 'clear'/>\n";
			echo "</div>\n";
			echo "<div id = 'content_historique'></div>\n";
			echo "<script type = 'text/javascript'>historique_presse(\"$periode\",\"$historique->periode\")</script>\n";
			echo "<div id = 'search_article'>";
		}
		$historique->titre = (ereg("<h2>(.*)</h2>",$historique->corps,$regs)) ? $regs[1] : $historique->titre;
		$historique->corps = (ereg("<h2>.*</h2>(.*)",$historique->corps,$regs)) ? $regs[1] : $historique->corps;		
		echo "<div class = 'info'>\n";
		echo "<div class = 'info_title'>$historique->titre</div>\n";
		echo "<div class = 'info_journal'>$historique->journal<span class = 'info_date'>".(format_date($historique->date))."</span></div>\n";	
		echo "<div class = 'info_corps' id = 'historique_$historique->id'>".(prepare_info($historique->corps))."</div>\n";
		echo "</div>\n";		
		if($search){
			echo "<script type = 'text/javascript'>$('search_article').scrollTo();</script>\n";
			echo "</div>\n";
			pied();
		} 
	}
}
else{
	$script = "function historique_periode(periode){\n";
	$script .= "	text = file('historique.php?periode='+periode);\n";
	$script .= "	writediv(text,'content_historique');\n";
	$script .= " 	var years = []\n";
	foreach($periodes as $year => $suffix){
		$script .= "		years.push('$year')\n";
	}
	$script .= "years.each(function(year){\n";
	$script .= "	if(year == periode) new Effect.Appear($('img_periode_'+year).up('li'),{to: 1,duration: 0})\n";
	$script .= "		else new Effect.Fade($('img_periode_'+year).up('li'),{to: 0.3,duration: 0})\n";
	$script .= "})\n";
	$script .= "}\n";

	$script .= "function historique_presse(periode,saison){\n";
	$script .= "	text = file('historique.php?periode='+periode+'&saison='+saison);\n";
	$script .= "	writediv(text,'content_historique');	\n";
	$script .= "	self.location.href = '#top';\n";
	$script .= "}\n";

	
	entete('historique',$script);
	echo "<h1>Historique</h1>\n";
	echo "<div id = 'bouton_historique'>\n";
	echo "<ol class = 'boutons'>\n";
	foreach($periodes as $year => $suffix){
		echo "<li class = 'bouton pointer' onclick = 'historique_periode(\"$year\")'>$year<br /><img  id = 'img_periode_$year' src = 'images/layout/historique$suffix.png' alt  = '$year' width = '60' height = '60'  style = 'cursor: pointer' /></li>\n";
	}
	echo "</ol>\n";
	echo "<br class = 'clear'/>\n";
	echo "</div>\n";
	echo "<div id = 'content_historique' style = 'padding-top: -20px'><p class = 'bold center'>Le Volleyball Club Lutry-Lavaux de 1976 &agrave; nos jours...<br />ou l'histoire d'un club r&eacute;gional qui s'est profil&eacute; avec succ&egrave;s au niveau national.</p>\n";
	echo "<p class = 'center fade' style = 'margin-top: 50px'>cliquez sur un logo pour acc&eacute;der &agrave; l'historique de la p&eacute;riode correspondante</p>\n";
	echo "</div>\n";

	pied();	
}
?>
